const { Connection, PublicKey } = require('@solana/web3.js');

async function checkBondingCurve() {
  try {
    const connection = new Connection(
      'https://api.devnet.solana.com',
      'confirmed',
    );
    const tokenMint = new PublicKey(
      '8CxVpvTe3jCMrjh3cW4pZv1oRbitzmadUv9QiqthuHdT',
    );
    const programId = new PublicKey(
      'DW6xwBgnBY6MUf7J4i8FwaqmncfvbKvW9hN9HmCyhDLH',
    );

    const [bondingCurvePda] = PublicKey.findProgramAddressSync(
      [Buffer.from('curve'), tokenMint.toBuffer()],
      programId,
    );

    console.log('联合曲线PDA:', bondingCurvePda.toString());

    const accountInfo = await connection.getAccountInfo(bondingCurvePda);
    if (!accountInfo) {
      console.log('❌ 找不到联合曲线账户');
      return;
    }

    const data = accountInfo.data;
    console.log('账户数据长度:', data.length);

    let offset = 8 + 32 + 32; // 跳过 discriminator + mint + creator
    const totalSupply = data.readBigUInt64LE(offset);
    offset += 8;
    const circulatingSupply = data.readBigUInt64LE(offset);
    offset += 8;
    const solReserves = data.readBigUInt64LE(offset);
    offset += 8;
    const isGraduated = data.readUInt8(offset) === 1;

    console.log('\n原始数据:');
    console.log('  totalSupply (raw):', totalSupply.toString());
    console.log('  circulatingSupply (raw):', circulatingSupply.toString());
    console.log('  solReserves (raw lamports):', solReserves.toString());
    console.log('  isGraduated:', isGraduated);

    console.log('\n转换后:');
    console.log('  totalSupply:', Number(totalSupply) / 1_000_000_000);
    console.log(
      '  circulatingSupply:',
      Number(circulatingSupply) / 1_000_000_000,
    );
    console.log('  solReserves (SOL):', Number(solReserves) / 1_000_000_000);

    // 检查联合曲线PDA的SOL余额
    const solBalance = await connection.getBalance(bondingCurvePda);
    console.log('\n联合曲线PDA的实际SOL余额:');
    console.log('  余额 (lamports):', solBalance);
    console.log('  余额 (SOL):', solBalance / 1_000_000_000);
  } catch (error) {
    console.error('错误:', error.message);
  }
}

checkBondingCurve();
